<?php require_once dirname(__FILE__) . "/shared/config/config.php"; ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-type" value="text/html; charset=UTF-8" />
        <title>Working Papers - Tesdo</title>
        <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php'; ?>
        <meta name="viewport" content="width=device-width" />
        <?php
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_paper_category.php";
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_working_papers.php";
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . "lib_pager.php";

        $working_paper_access = new dao_working_paper();
        $paper_category_access = new dao_paper_category();
        $lib_pager = new lib_pager();

        $default_title = 'Working Paper Series';
        $paper_category_title = '';
        $default_description = $working_paper_access->get_default_description();

        if (!empty($_GET['paper_cat_id'])) {
            $paper_category_id = $_GET['paper_cat_id'];
            $paper_category_title = $paper_category_access->get_name_by_id($paper_category_id);
            $papers_archive = $working_paper_access->get_all_archive_by_paper_category_id($paper_category_id);
            $papers = $working_paper_access->get_all_by_paper_category_id($paper_category_id);
            $default_description['paper_default_description'] = '';
        } else if (!empty($_GET['arc_paper_cat_id'])) {
            $paper_category_id = $_GET['arc_paper_cat_id'];
            if (!empty($_GET['month'])) {
                $month = $_GET['month'];
            }
            if (!empty($_GET['year'])) {
                $year = $_GET['year'];
            }
            $paper_category_title = $paper_category_access->get_name_by_id($paper_category_id);
            $papers_archive = $working_paper_access->get_all_archive_by_paper_category_id($paper_category_id);
            $papers = $working_paper_access->get_all_by_paper_category_id_and_date($paper_category_id, $month, $year);
            $default_description['paper_default_description'] = '';
        } else if (!empty($_GET['paper_id'])) {
            $paper_id = $_GET['paper_id'];
            $paper = $working_paper_access->get_by_id($paper_id);
            $paper_category_id = $paper['cat_id'];
            $paper_category_title = $paper_category_access->get_name_by_id($paper_category_id);
            $papers_archive = $working_paper_access->get_all_archive_by_paper_category_id($paper_category_id);
            $default_description['paper_default_description'] = '';
        }

        $paper_categories = $paper_category_access->get_all();
        ?>
    </head>
    <body>
        <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'header.php'; ?>

        <!-- Body -->
        <div id="body">
            <section class="content-wrapper main-content clear-fix">
                <div class="DetailPage">
                    <hgroup class="title">
                        <h1>
                            <span id="MainContent_lblTitle">
                                <?php echo $default_title; ?>
                            </span></h1>
                        <h2></h2>
                    </hgroup>
                    <div class="LeftArea">
                        <div id="left_menu">
                            <div>
                                <table id="MainContent_dlcatList" cellspacing="0" style="border-collapse:collapse;">
                                    <tbody>
                                        <?php
                                        foreach ($paper_categories as $i => $item) {
                                            ?>
                                            <tr>
                                                <td>
                                                    <ul>
                                                        <li><a href="working_papers.php?paper_cat_id=<?php echo $item['cat_id']; ?>"><?php echo $item['cat_name']; ?></a></li>
                                                    </ul>
                                                </td>
                                            </tr>
                                            <?php
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <article class="CenterArea adjust_center">
                        <h2><?php echo $paper_category_title; ?></h2>
                        <?php
                        echo $default_description['paper_default_description'];
                        ?>
                        <?php
                        if (!empty($_GET['arc_paper_cat_id']) || !empty($_GET['paper_cat_id'])) {
                            if (empty($papers) || sizeof($papers) <= 0) {
                                echo "Not papers found.";
                            } else {
                                $current_page = 1;
                                $page_size = 10;
                                if (!empty($_GET['p'])) {
                                    $current_page = $_GET['p'];
                                }

                                $list_total = $papers;
                                $total_page = $lib_pager->get_total_page($list_total, $page_size);
                                $current_page_list = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);

                                $count = 0;
                                foreach ($current_page_list as $i => $item) {
                                    ?>
                                    <table id="MainContent_dlPapers" cellspacing="0" cellpadding="0" style="border-collapse:collapse;">
                                        <tbody><tr>
                                                <td>
                                                    <div class="adsSortDetail">
                                                        <div>
                                                            <b> <a href="working_papers.php?paper_id=<?php echo $item["PaperID"]; ?>" style="color:blue; font-size:14px;"><?php echo $item["Title"]; ?></a></b><br>
                                                            <b>Author:</b> <?php echo $item["Author"]; ?> <br>
                                                            <b>Keywords:</b> <?php echo $item["Keywords"]; ?> <br>
                                                            <b>No of Papers:</b> <?php echo $item["Pages"]; ?><br>
                                                            <hr>
                                                            <b><a href="working_papers.php?paper_id=<?php echo $item["PaperID"]; ?>" style="color:blue; font-size:14px;">Abstract</a> | <a style="color:blue; font-size:14px;" target="_blank" href="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_PDF_PAPERS . $item["PDFFiles"]; ?>">PDF</a></b>
                                                            <hr>
                                                        </div>
                                                    </div></td>
                                            </tr>
                                        </tbody></table>
                                    <?php
                                }
                                
                                if (!empty($_GET['paper_cat_id'])) {
                                    $params = "&paper_cat_id=" . $paper_category_id;
                                } else if (!empty($_GET['arc_paper_cat_id'])) {
                                    $params = "&arc_paper_cat_id=" . $paper_category_id . "&month=" . $month . "&year=" . $year;
                                }
                                ?>
                                <table>
                                    <tbody>
                                        <tr>
                                            <?php for ($a = 1; $a <= $total_page; $a++) { ?>
                                                <td>
                                                    <span>
                                                        <a href="working_papers.php?p=<?php echo $a; echo $params; ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
                                                    </span>
                                                </td>
                                            <?php } ?>
                                        </tr>
                                    </tbody>
                                </table>
                                
                                <?php
                            }
                        }
                        ?>

                        <?php
                        if (!empty($_GET['paper_id'])) {
                            $item = $paper;
                            ?>
                            <div id="MainContent_AbstractDeail" class="AbstractDeail">
                                <h4><span id="MainContent_lblAbsTitle"><?php echo $item["Title"]; ?></span></h4>
                                <hr>
                                <h5>Author:</h5>
                                <p> <span id="MainContent_lblAuthor"><?php echo $item["Author"]; ?> </span></p>
                                <h5>Citation:</h5>
                                <p>  <span id="MainContent_lblCitation"><?php echo $item["Citation"]; ?></span></p>
                                <h5>Keywords:</h5>
                                <p>   <span id="MainContent_lblKeywords"><?php echo $item["Keywords"]; ?></span></p>
                                <h5>Article Type:</h5>
                                <p>   <span id="MainContent_lblArticleType"><?php echo $item["ArticleType"]; ?></span></p>
                                <h5>Pages:</h5>
                                <p> <span id="MainContent_lblPages"><?php echo $item["Pages"]; ?></span></p>
                                <h5>DOI:</h5>
                                <p>  <span id="MainContent_lblDOI"><?php echo $item["DOI"]; ?></span></p>
                                <h5>Publisher:</h5>
                                <p>  <span id="MainContent_lblPublisher"><?php echo $item["Publisher"]; ?></span></p>
                                <h5>Abstract:</h5>
                                <p> <span id="MainContent_lblAbstract"><?php echo $item["Abstract"]; ?></span></p>

                                <hr>
                                <a id="MainContent_PDFFile" title="Download PDF File" href="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_PDF_PAPERS . $item["PDFFiles"]; ?>" target="_blank">Download PDF File</a>

                            </div>
                            <?php
                        }
                        ?>
                    </article>
                    <aside>
                    <a href="mailto:papers@tesdo.org">
                    <input type="image" name="ctl00$MainContent$ImageButton1" id="MainContent_ImageButton1" class="btnSubmitArticle" src="<?php echo LINK_ROOT . DIR_LAYOUT_IMAGES . "SubmitArticle.png"?>" style="width:190px;">
                    </a>
                    
                    <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'navbar.php'; ?>
                    <div class="RightArea jump">
                        
                        <div id="Archive">
                            <h1>Archive</h1>
                            <div>
                                <table id="MainContent_dlArchive" cellspacing="0" style="border-collapse:collapse;">
                                    <tbody>
                                        <tr>
                                            <td>
                                                <ul>
                                                    <?php
                                                    if (!empty($papers_archive) && sizeof($papers_archive) > 0) {
                                                        foreach ($papers_archive as $i => $item) {
                                                            ?>
                                                            <li>
                                                                <a href="working_papers.php?arc_paper_cat_id=<?php echo $item["cat_id"]; ?>&month=<?php
                                                                echo $item['month'];
                                                                ;
                                                                ?>&year=<?php echo $item['year']; ?>">
                                                                       <?php echo $item['date_string']; ?>
                                                                </a>
                                                            </li>
                                                            <?php
                                                        }
                                                    }
                                                    ?>
                                                </ul>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                        </aside>
                </div>
            </section>
        </div>
        <!-- End Body -->

        <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php'; ?>
    </body>
</html>